package com.o.mj.service;

import com.o.mj.dao.JedisDao;
import com.o.mj.dao.WorkPersonMapper;
import com.o.mj.pojo.UserInfo;
import com.o.mj.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {
    @Autowired
    private WorkPersonMapper workPersonMapper;
    @Autowired
    private JedisPool jedisPool;
    @Value("NEW_servicew")
    private String REDIS_USERS_PRIFX;

    public UserInfo find(String userId) {

        try {
            Jedis resource = jedisPool.getResource();
            String json = resource.get(this.REDIS_USERS_PRIFX + ":" + userId);
            if (json != null && json.length() > 0) {
                System.out.println("---查询redis---!");
                UserInfo userInfo = JsonUtils.jsonToPojo(json, UserInfo.class);
                return userInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        UserInfo userInfo = this.workPersonMapper.getPersonMessage(userId);
        System.out.println(userInfo);
        System.out.println("---放入redis--");
        try {
            String objectToJson = JsonUtils.objectToJson(userInfo);
            Jedis resource = jedisPool.getResource();
            resource.set(this.REDIS_USERS_PRIFX + ":" + userId, objectToJson);
            resource.expire(this.REDIS_USERS_PRIFX + ":" + userId, 30);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfo;

    }

}
